//
//  LGSportsEvaDetailController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/11.
//

#import "LGSportsEvaDetailController.h"
//心肺测试--统计图
#import "LGSportsEvaChartController.h"
#import "LGSportsEvaDetailCpetCell.h"
#import "LGSportsEvaDetailTextCell.h"
#import "LGSportsEvaDetailSelectCell.h"
#import "LGRecoverySectionView.h"
@interface LGSportsEvaDetailController ()<UITableViewDelegate,UITableViewDataSource,LGSportsEvaDetailTextDelegate,LGSportsEvaDetailSelectDelegate>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UILabel *dateLabel;
/**   */
@property (nonatomic , strong)NSArray *cpetArray;
/**   */
@property (nonatomic , strong)NSArray *dataArray;

@end

@implementation LGSportsEvaDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"运动评估";
    [self.view addSubview:self.tableView];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/sport.estimate/detail" parameters:@{@"patient_id":self.patientid,@"date":self.dateStr} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.cpetArray = returnData[@"cpet_report"];
        self.dataArray = returnData[@"question"];
        [self.tableView reloadData];
    } error:^(NSError *error) {
        
    }];
}

-(void)checkRecordChart:(NSString *)name{
    LGSportsEvaChartController *controller = [[LGSportsEvaChartController alloc]init];
    controller.patientid = self.patientid;
    controller.name = name;
    controller.type = @"question";
    [self.navigationController pushViewController:controller animated:YES];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSInteger count = (self.cpetArray.count>0)?self.cpetArray.count+1:0;
    return self.dataArray.count+count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(self.cpetArray.count>0){
        //有心肺运动测试
        if(section == 0){
            return 0;
        }else if(section-1<self.cpetArray.count){
            NSArray *tempArray = self.cpetArray[section-1][@"items"];
            return tempArray.count;
        }else{
            NSDictionary *tempDic = self.dataArray[section-self.cpetArray.count-1];
            return [tempDic[@"content"] count];
        }
    }else{
        NSDictionary *tempDic = self.dataArray[section];
        return [tempDic[@"content"] count];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.cpetArray.count>0){
        if(indexPath.section>0 && indexPath.section-1<self.cpetArray.count){
            LGSportsEvaDetailCpetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cpetCell" forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.dataDic = self.cpetArray[indexPath.section-1][@"items"][indexPath.row];
            return cell;
            
        }else{
            NSDictionary *tempDic = self.dataArray[indexPath.section-self.cpetArray.count-1][@"content"][indexPath.row];
            NSString *type = LGNSString(tempDic[@"type"]);
            if([type isEqualToString:@"text"]){
                LGSportsEvaDetailTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.dataDic = tempDic;
                cell.delegate = self;
                return cell;
            }else{
                LGSportsEvaDetailSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"selectCell" forIndexPath:indexPath];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.dataDic = tempDic;
                cell.delegate = self;
                return cell;
            }
        }
    }else{
        NSDictionary *tempDic = self.dataArray[indexPath.section][@"content"][indexPath.row];
        NSString *type = LGNSString(tempDic[@"type"]);
        if([type isEqualToString:@"text"]){
            LGSportsEvaDetailTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.dataDic = tempDic;
            cell.delegate = self;
            return cell;
        }else{
            LGSportsEvaDetailSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"selectCell" forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.dataDic = tempDic;
            cell.delegate = self;
            return cell;
        }
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.cpetArray.count>0 && indexPath.section>0 && indexPath.section-1<self.cpetArray.count){
        NSDictionary *tempDic = self.cpetArray[indexPath.section-1][@"items"][indexPath.row];
        LGSportsEvaChartController *controller = [[LGSportsEvaChartController alloc]init];
        controller.patientid = self.patientid;
        controller.name = LGNSString(tempDic[@"title"]);
        controller.type = @"cpet_report";
        [self.navigationController pushViewController:controller animated:YES];
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(self.cpetArray.count>0 && section>0 && section-1<self.cpetArray.count){
        return viewPix(40);
    }else{
        return viewPix(51);
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if(self.cpetArray.count>0 && section>0 && section-1<self.cpetArray.count){
        //心肺运动测试
        NSDictionary *tempDic = self.cpetArray[section-1];
        LGCornerHeaderView *headView = [[LGCornerHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(40))];
        headView.topMargin = -viewPix(10);
        LGRecoverySectionView *titleView = [[LGRecoverySectionView alloc]initWithFrame:CGRectMake(viewPix(16), 0, Screen_W-viewPix(32), viewPix(40))];
        titleView.title = LGNSString(tempDic[@"report_title"]);
        [headView addSubview:titleView];
        return headView;
    }else{
        
        NSString *title;
        if(self.cpetArray.count>0){
            title = (section == 0)?@"心肺运动测试":LGNSString(self.dataArray[section-self.cpetArray.count-1][@"title"]);
        }else{
            title = LGNSString(self.dataArray[section][@"title"]);
        }

        UIImage *image = [[UIImage imageNamed:@"就诊检查-蓝色"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 20, 5, 20) resizingMode:UIImageResizingModeStretch];
        CGFloat textWidtht = [title textWidthWithFont:LGFontWeight(16, UIFontWeightMedium) maxHeight:MAXFLOAT];
        LGCornerHeaderView *headView = [[LGCornerHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(51))];
        headView.topMargin = viewPix(8);
        UIButton *button = [UIButton buttonWithTitle:title titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#1F6CC6" imageName:@""];
        button.frame = CGRectMake(viewPix(16), viewPix(20), textWidtht+viewPix(32), viewPix(27));
        [button setBackgroundImage:image forState:UIControlStateNormal];
        button.userInteractionEnabled = NO;
        [headView addSubview:button];
        return headView;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if(self.cpetArray.count>0 && section<self.cpetArray.count){
        return 0.001;
    }
    return viewPix(15);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if(self.cpetArray.count>0 && section<self.cpetArray.count){
        return [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.001)];
    }
    LGCornerFooterView *footView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(15))];
    footView.bottomMargin = viewPix(8);
    return footView;;
}



-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGSportsEvaDetailCpetCell class] forCellReuseIdentifier:@"cpetCell"];
        [_tableView registerClass:[LGSportsEvaDetailTextCell class] forCellReuseIdentifier:@"textCell"];
        [_tableView registerClass:[LGSportsEvaDetailSelectCell class] forCellReuseIdentifier:@"selectCell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [self tableHeaderView];
    }
    return _tableView;
}

-(UILabel *)dateLabel{
    if(!_dateLabel){
        _dateLabel = [UILabel lableWithText:[NSString stringWithFormat:@"评估日期：%@",self.dateStr] colorString:@"#333333" textFont:LGFont(15) textAlignment:NSTextAlignmentRight lines:1];
    }
    return _dateLabel;
}

-(UIView *)tableHeaderView{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(46))];
    headView.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
    
    UILabel *titleLabel = [UILabel lableWithText:@"运动评估数据" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    titleLabel.frame = CGRectMake(viewPix(17), viewPix(16), viewPix(110), viewPix(22));
    [headView addSubview:titleLabel];
    [headView addSubview:self.dateLabel];
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(headView).offset(-viewPix(16));
        make.centerY.equalTo(titleLabel);
    }];
    
    return headView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
